//
//  CoreAnimationVC.m
//  SYPerfectDemo
//
//  Created by Lssyyy on 2018/10/15.
//  Copyright © 2018年 Lssyyy. All rights reserved.
//

#import "CoreAnimationVC.h"

@interface CoreAnimationVC ()
@property (nonatomic,strong) UIView *redView;
@end

@implementation CoreAnimationVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
}

- (void)addUI{
    self.redView = [[UIView alloc]initWithFrame:CGRectMake(100, 150, 100, 100)];
    self.redView.backgroundColor = [UIColor redColor];
    [self.view addSubview:self.redView];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//    [self basicAnimation];
//    [self keyFrameAnimation];
    [self Scale];
}

- (void)Scale{
    UIImageView *iv = [UIImageView new];
    iv.image = [UIImage imageNamed:@"bg_zss"];
    iv.frame = CGRectMake(200, 200, 200, 200);
    [self.view addSubview:iv];
    
    UIButton *button = [UIButton buttonWithType:0];
    [button setImage:[UIImage imageNamed:@"icon_rob"] forState:0];
    [button setTitle:@"X2" forState:0];
    [button.titleLabel setFont:[UIFont systemFontOfSize:20]];
    button.frame = CGRectMake(50, 50, 100, 100);
    [iv addSubview:button];
    
    CABasicAnimation* rotationAnimation;
    
     rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.y"];
    
    rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 ];
    
    rotationAnimation.duration = 2;
    
    rotationAnimation.cumulative = YES;
    
    rotationAnimation.repeatCount = 2;
    
    [button.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];//开始动画
    
//    [self performSelector:@selector(removeIV:) withObject:iv afterDelay:2.0];
}

- (void)removeIV:(UIImageView *)iv{
    [iv removeFromSuperview];
    iv = nil;
}

 //CABasicAnimation 基本动画
- (void)basicAnimation{
    CABasicAnimation *base = [CABasicAnimation animationWithKeyPath:@"transform.scale.x"];
    /*在动画结束之后是否让动画回到原处，这个属性的默认值是YES(回到原处),此时fillMode是没有作用的
     如果设置为NO那么就需要设置一个fillMode属性，就是动画结束之后的状态，如果不设置，动画也会回到原处。*/
    base.removedOnCompletion = NO;
    //动画结束之后状态的设置
    base.fillMode = kCAFillModeForwards;
    base.fromValue = @(2);
    //    base.toValue = @(2);
    //    base.byValue = @(3);
    //动画时长
    base.duration = 2;
    //动画的重复次数
    base.repeatCount = 3;
    //给base动画设置延时启动
    //    base.beginTime = 2 + CACurrentMediaTime();
    //动画是否按原路径返回
    base.autoreverses = YES;
    //将动画添加到layer层forKey*是给动画添加一个标记，方便删除。
    [self.redView.layer addAnimation:base forKey:@"basic"];
}
//CAKeyFrameAnimation关键帧动画
- (void)keyFrameAnimation{
    CAKeyframeAnimation *keyFram = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    
    //layer最初的position值
    float position_x = self.redView.layer.position.x;
    float position_y = self.redView.layer.position.y;
    
    //layer向左晃动的偏移量
    NSValue *leftValue = [NSValue valueWithCGPoint:CGPointMake(position_x - 30, position_y-30)];
    //layer的原始位置
    NSValue *originValue = [NSValue valueWithCGPoint:CGPointMake(position_x, position_y)];
    //layer向右晃动的偏移量
    NSValue *rightValue = [NSValue valueWithCGPoint:CGPointMake(position_x + 30, position_y+30)];
    //添加每一帧的Value值
    [keyFram setValues:@[originValue,leftValue,originValue,rightValue,originValue]];
    keyFram.repeatCount = 10;
    keyFram.repeatDuration = 1;
    //为layer层添加动画
    [self.redView.layer addAnimation:keyFram forKey:@"aaa"];
}

@end
